#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/joystick.h>


#define JOY_DEV "/dev/input/js0"

int ac ;
int main()
{
	int joy_fd, *axis=NULL, num_of_axis=0, num_of_buttons=0, x;
	char *button=NULL, name_of_joystick[80];
	struct js_event js;

	if( ( joy_fd = open( JOY_DEV , O_RDONLY)) == -1 )
	{
		printf( "Couldn't open joystick\n" );
		return -1;
	}

	ioctl( joy_fd, JSIOCGAXES, &num_of_axis );
	ioctl( joy_fd, JSIOCGBUTTONS, &num_of_buttons );
	ioctl( joy_fd, JSIOCGNAME(80), &name_of_joystick );

	axis = (int *) calloc( num_of_axis, sizeof( int ) );
	button = (char *) calloc( num_of_buttons, sizeof( char ) );

	printf("Joystick detected: %s\n\t%d axis\n\t%d buttons\n\n"
		, name_of_joystick
		, num_of_axis
		, num_of_buttons );

	fcntl( joy_fd, F_SETFL, O_NONBLOCK );	/* use non-blocking mode */

	while( 1 ) 	/* infinite loop */
	{

			/* read the joystick state */
		read(joy_fd, &js, sizeof(struct js_event));
		
			/* see what to do with the event */
		switch (js.type & ~JS_EVENT_INIT)
		{
			case JS_EVENT_AXIS:
				axis   [ js.number ] = js.value;
				break;
			case JS_EVENT_BUTTON:
				button [ js.number ] = js.value;
				break;
		}
        

	if (axis[0]>0){
		
	   printf("R \n");}
	
	if (axis[0]<0){
		
	   printf("L \n");}

	if (axis[1]>0){
		
	   printf("Down \n");}
	
	if (axis[1]<0){
		
	   printf("Up \n");}

        

	/*if (axis[2]>0){
		
	   printf("R2 \n");}
	
	if (axis[2]<0){
		
	   printf("L2 \n");}*/

	if (axis[3]>0){
		
	   printf("R 2 \n");}
	
	if (axis[3]<0){
		
	   printf("L 2 \n");}


        

	if (axis[4]>0){
		
	   printf("Down 2 \n");}
	
	if (axis[4]<0){
		
	   printf("Up 2 \n");}

	if (axis[5]>0){
		
	   printf("Down5 \n");}
	
	if (axis[5]<0){
		
	   printf("Up5 \n");}

        

	if (axis[6]>0){
		
	   printf("R6 \n");}
	
	if (axis[6]<0){
		
	   printf("L6 \n");}

	if (axis[7]>0){
		
	   printf("Down7 \n");}
	
	if (axis[7]<0){
		
	   printf("Up7 \n");}



        
	axis[1]=0;
	axis[0]=0;
	axis[2]=0;
	axis[3]=0;
	axis[4]=0;
	axis[5]=0;
	axis[6]=0;
	axis[7]=0;
	

			/* print the results */
/*

		printf( "X: %6d  Y: %6d  ", axis[0], axis[1] );
		
		if( num_of_axis > 2 )
			printf("Z: %6d  ", axis[2] );
			
		if( num_of_axis > 3 )
			printf("R: %6d  ", axis[3] );
			
		for( x=0 ; x<num_of_buttons ; ++x )
			printf("B%d: %d  ", x, button[x] );

		printf("  \r"); */



int m;
  char n[48];
  FILE * pFile;
  pFile = fopen ("./myfile.txt","r");
  if (pFile!=NULL)
  {
    fputs ("12",pFile);
    fgets (n , 48 , pFile);
    

    m=atoi(n);



    fclose (pFile);
  }



int r;
r=0;

if (m==1 && button[0]>0){
        printf("Opning the door");
	system ("./run1.sh");
	

while(r<=3){
	//system ("./sleep.sh");
r=r+1 ;

}; system ("./1.sh"); 
}


if (button[1]>0){
system ("./mu.sh");
while(r<=3){
	//system ("./sleep.sh");
r=r+1 ;

};  
}

if (button[2]>0){
printf(" pouchet2");
}
if (button[3]>0){
printf(" pouchet3");
}
if (button[4]>0){
printf(" pouchet4");
}
if (button[5]>0){
printf(" pouchet5");
}
if (button[6]>0){
printf(" pouchet6");
}
if (button[7]>0){
printf(" pouchet7");
}
if (button[8]>0){
printf(" pouchet8");
}
if (button[9]>0){
printf(" pouchet9");
}
if (button[10]>0){
printf(" pouchet10");
}
if (button[11]>0){
printf(" pouchet11");}
if (button[12]>0){
printf(" pouchet12");}


		fflush(stdout);

	}

	close( joy_fd );	/* too bad we never get here */
	return axis[0];
}
	
